package com.inews.business.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.exception.ExceptionHandler;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExcelUtil;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;

import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;


import com.inews.business.entity.TblRetireStaff;
import com.inews.business.entity.TblSysUser;
import com.inews.business.entity.TblTeacher;
import com.inews.business.entity.TblTrainInfo;

import com.inews.business.repository.ITeacherDao;
import com.inews.business.vo.RetireStaffVo;
import com.inews.business.vo.SalaryChangeVo;
import com.inews.business.vo.TeacherVo;
import com.inews.business.vo.TrainInfoVo;
@Service
public class TeacherServiceImpl implements  ITeacherService, InewsBusinessConstants{

	@Autowired
	private ITeacherDao teacherDao;
	
	@Autowired
	private BaseService baseService;
	
	private static Logger logger = Logger.getLogger(TeacherServiceImpl.class);


	@Override
	public ExtData<TeacherVo> findTeacherPage(Map<String, Object> filterParams,
			Pageable page) {
		List<TeacherVo> voList = new ArrayList<TeacherVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, TEACHER_STATUS_NORMAL));
		Page<TblTeacher> pagePoList = teacherDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblTeacher.class), page);
		for (TblTeacher po : pagePoList.getContent()) {
			TeacherVo vo = BeanMapper.map(po, TeacherVo.class);
			voList.add(vo);
		}
		
		ExtData<TeacherVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}


	@Override
	@Transactional(readOnly=false)
	public ExtData<TeacherVo> saveTeacher(TeacherVo vo, BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTeacher po = BeanMapper.map(vo, TblTeacher.class);
		po.setStatus(TEACHER_STATUS_NORMAL);		
        po.setCreateUser(editUser);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_ADD);
		po.setLastUpdTime(currentTime);
		po.setCreateTime(currentTime);
		teacherDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}


	@Override
	@Transactional(readOnly=false)
	public ExtData<TeacherVo> updateTeacher(TeacherVo vo, BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTeacher po = teacherDao.findOne(vo.getStaffId());	
		BeanMapper.copy(vo,po);		
		po.setLastUpdUser(editUser);
		po.setStatus(TEACHER_STATUS_NORMAL);
		po.setLastUpdDes(LAST_UPD_DES_UPDATE);
		po.setLastUpdTime(currentTime);
		po.setCreateTime(currentTime);
		teacherDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}


	@Override
	@Transactional(readOnly=false)
	public ExtData<TeacherVo> deleteTeacher(TeacherVo vo, BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTeacher po = teacherDao.findOne(vo.getStaffId());
		po.setStatus(TEACHER_STATUS_DELETE);
       po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_DELETE);
		po.setLastUpdTime(currentTime);
		teacherDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	@Override
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<TeacherVo> exportExcel(Map<String, Object> filterParams){
		
		
		List<TeacherVo> voList = new ArrayList<TeacherVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, TEACHER_STATUS_NORMAL));
		List<TblTeacher> pagePoList = teacherDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblTeacher.class));
		for (TblTeacher po : pagePoList) {
			TeacherVo vo = BeanMapper.map(po, TeacherVo.class);
			voList.add(vo);
		}
		return ExtDataUtil.genWithData(voList);
	}
	
	
	
	@Override
	/**
	 * 导入excel.
	 *
	 * @param excelFile the excel file
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<String> importExcel(InputStream is, BaseUserVo user){
		
		try {
			List<Object> voList =ExcelUtil.ImportVo(is, ExcelUtil.TeacherValue, TeacherVo.class);
			 for (Object obj : voList) {
				 TeacherVo vo=(TeacherVo)obj;
	   	if(teacherDao.exists(vo.getStaffId())){
							updateTeacher(vo, user);
						}else{
							saveTeacher(vo, user);
						}
				}
				return ExtDataUtil.genWithSingleData("导入成功");
		} catch (Exception e) {		   
			throw new BusinessException("导入失败 原因是"+e.getMessage());
		}
	}
	
	
	/**
	 * 设置教师信息.
	 *
	 * @param <T> the generic type
	 * @param t the t
	 * @param teacher the teacher
	 * @return the t
	 */
	public <T> T setTeacherInfo(T t, String staffId){
		TblTeacher teacher = teacherDao.findByStaffId(staffId);
		if(InewsUtils.isNotEmpty(teacher)){
			BeanMapper.copy(teacher, t);
		}
		return t;
	}
	
	
}	
	
